package edu.lyun.trainingproject.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Objects;
import java.util.Properties;

/**
 * @Title: PropertiesConfigLoader
 * @Author: CokeGGts
 * @Date: 2024-10-17 21:31:25
 * @Description: 配置文件读取单例类, 实现方式为饿汉模式
 */
public class PropertiesConfigLoader {
    /** @Description : 实际参数 */
    private static volatile PropertiesConfigLoader instance;
    /** @Description : Properties映射表 */
    private HashMap<String, Properties> propertiesMap;
    /** @Description : 描点配件, 用于定位Resources文件夹, 无其他特殊作用 */
    private String anchorsFileName;
    boolean isLoad;

    private void init() {
        anchorsFileName = "/anchors.conf";
        propertiesMap = new HashMap<>();
        isLoad = false;
    }

    public static PropertiesConfigLoader getInstance() {
        if (instance == null) {
            synchronized (PropertiesConfigLoader.class) {
                //双重判空避免多线程访问问题
                if (instance == null) instance = new PropertiesConfigLoader();
            }
        }
        return instance;
    }

    private PropertiesConfigLoader() {
        init();
    }

    /**
     * @Title: loadPropertiesConfigFile
     * @Author: CokeGGts
     * @Date: 2024-10-17 21:26:05
     * @Params: []
     * @Return: void
     * @Description: 加载配置Properties配置文件
     */
    public HashMap<String, Properties> loadPropertiesConfigFileToMap() {
        if (isLoad) return this.propertiesMap;
        String resourcesPath = Objects.requireNonNull(getClass().getResource(anchorsFileName)).getFile();
        resourcesPath = resourcesPath.substring(0, resourcesPath.lastIndexOf('/'));
        System.out.println(resourcesPath);
        File file = new File(resourcesPath);
        for (File listFile : Objects.requireNonNull(file.listFiles())) {
            if (listFile.isDirectory()) continue;
            String name = listFile.getName();
            //避免读取到某些没有扩展名文件而发生错误
            try {
                int lostDotIdx = name.lastIndexOf('.');
                String suffix = name.substring(lostDotIdx + 1);
                if (!suffix.equals("properties")) continue;
                String prefix = name.substring(0, lostDotIdx);
                try (InputStream inputStream = new FileInputStream(listFile)) {
                    Properties properties = new Properties();
                    properties.load(inputStream);
                    this.propertiesMap.put(prefix, properties);
                }
            } catch (Exception _) {
                //Do nothing
            }
        }
        isLoad = true;
        return this.propertiesMap;
    }

    /**
     * @Title: reLoadPropertiesConfigFileToMap
     * @Author: CokeGGts
     * @Date: 2024-10-17 22:14:53
     * @Params: []
     * @Return: HashMap<String, Properties>
     * @Description: 重新加载Properties配置文件
     */
    public HashMap<String, Properties> reLoadPropertiesConfigFileToMap() {
        isLoad = false;
        this.propertiesMap.clear();
        return loadPropertiesConfigFileToMap();
    }
}
